﻿
using Events.EventsInfo;
using UnityEngine;

public class PlayerGroundState:PlayerStateBase
{
    public PlayerGroundState(ControllerBase controller, StateMachineBase stateMachine, string stateName,string 
        animatorParameter) 
        : base(controller, stateMachine, stateName,animatorParameter)
    {
        //监听剑回收完成
        EventCenter.Instance.AddEventListener<ReturnSwordInfo>(E_EventType.Sword_FinishReturn, ReturnSword);
    }

    public override void Update()
    {
        base.Update();
        if (Input.GetKeyDown(KeyCode.Space))
        {
            stateMachine.ChangeState(Setting.jump);
            return;
        }
        if (Input.GetKeyDown(KeyCode.Q))//扔剑技能
        {
            //SwordSkilInput(Setting.SkillThrowSword);
            SwordSkilInput(Setting.curSwordSkill);//弹射剑
            //SwordSkilInput(Setting.SkillPierceSword);//穿透剑
            return;
        }

        if (Input.GetKeyDown(KeyCode.R))//释放黑洞技能
        {
            HoleSkillInput(Setting.curHoleSkill);
        }
        
        if (Input.GetMouseButtonDown(0))//普通攻击
        {
            stateMachine.ChangeState(Setting.primaryAttack);
            return;
        }
        if (Input.GetMouseButtonDown(1))//回击
        {
            stateMachine.ChangeState(Setting.counter);
            return;
        }

        if (!controller.isGround)
        {
            stateMachine.ChangeState(Setting.air);
        }
    }

    private bool CanUseSkill(SkillBase  skillBase)
    {
        return  skillBase.CanUseSkill();
    }
    private void HoleSkillInput(string skillName)
    {
        var skill = SkillManager.Instance
            .GetSkill<BlackHoleSkill>(skillName);
        if (!CanUseSkill(skill))//是否在进行技能是否在cd
        {
            return;
        }
        //进入释放黑洞的状态
        stateMachine.ChangeState(Setting.backHole);
    }

    private void SwordSkilInput(string skillName)
    {
        var skill = SkillManager.Instance
            .GetSkill<ThrowSwordSkill>(skillName);
        
        if (skill.hasSword)//有剑
        {
            //判断是否可以释放技能
            if (!CanUseSkill(skill))
            {
                return;
            }
            stateMachine.ChangeState(Setting.swordAim);
                
        }
        else //无剑回收剑
        {
            //剑是否落地可回收
            if (skill.canReturn)
            {
                //stateMachine.ChangeState(Setting.swordCatch);
                //触发回收剑
                EventCenter.Instance.EventTrigger(E_EventType.Sword_StartReturn);
            }
        }
    }

    private void ReturnSword(ReturnSwordInfo info)
    {
        if (!info.notEnterCatchState)
        {
            stateMachine.ChangeState(Setting.swordCatch,info);
            
        }
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
        EventCenter.Instance.RemoveEventListener<ReturnSwordInfo>(E_EventType.Sword_FinishReturn,ReturnSword);
    }
    
    
}